﻿using AutoPublish;
using AutoPublish.BackUpServices;
using Microsoft.Web.Administration;
using Quartz;
using Quartz.Impl;
using System.Text;

//string programFolder = "C:\\Code\\gitee\\temp\\WebApplication1\\WebApplication1";
//string publishFolder = "C:\\webSite\\publishFolder";
//string iisWebName = "T";
//string iisFolder = "C:\\webSite\\dotnetTest";
//string appPoolName = "dotnetCore";

//ServerManager serverManager = new ServerManager();
//var site = serverManager.Sites[iisWebName];
//ApplicationPool appPool = serverManager.ApplicationPools[appPoolName];
//CommandRunner dotnet = new CommandRunner("dotnet", programFolder);

//string r = "";
//r = dotnet.Run("publish -c Release -o " + publishFolder);
//File.Delete(publishFolder + "\\appsettings.json");

//if (site.State == ObjectState.Started)
//    site.Stop();
//if (appPool.State == ObjectState.Started)
//    appPool.Stop();
//serverManager.CommitChanges();

//CopyDirectory(publishFolder, iisFolder);

//appPool.Start();
//site.Start();
//serverManager.CommitChanges();
//Console.WriteLine("执行完毕");
//Console.ReadKey();
//return;

StdSchedulerFactory sf = new StdSchedulerFactory();

IScheduler sched = await sf.GetScheduler();
ITrigger trigger = TriggerBuilder.Create().WithIdentity("myTrigger", "group1").WithCronSchedule("0/10 * * * * ?").Build();
IJobDetail job = JobBuilder.Create<PublishJob>().WithIdentity("myJob", "group1").Build();
await sched.ScheduleJob(job, trigger);
await sched.Start();
Console.WriteLine("执行完毕");
Console.ReadKey();

void CopyDirectory(string srcDir, string destDir)
{
    if (!Directory.Exists(destDir))
    {
        Directory.CreateDirectory(destDir);
    }

    foreach (string file in Directory.GetFiles(srcDir))
    {
        string dest = Path.Combine(destDir, Path.GetFileName(file));
        File.Copy(file, dest, true);
    }

    foreach (string folder in Directory.GetDirectories(srcDir))
    {
        string dest = Path.Combine(destDir, Path.GetFileName(folder));
        CopyDirectory(folder, dest);
    }
}